package leetcode.code0838;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution implements IDebug {

	public String pushDominoes(String dominoes) {
		char[] cs = dominoes.toCharArray();
		int len = cs.length;
		int rsize = -1;
		for (int i = 0; i < len; i++) {
			char cur = cs[i];
			if (cur == 'R') {
				rsize = 0;
			} else if (cur == 'L') {
				this.back(cs, i, rsize);
				rsize = -1;
			} else {
				if (rsize >= 0) {
					cs[i] = 'R';
					rsize++;
				}
			}
		}
		return new String(cs);
	}

	private void back(char[] cs, int i, int size) {
		if (size == -1) {
			int p = i - 1;
			while (p >= 0 && cs[p] == '.') {
				cs[p--] = 'L';
			}
		} else {
			boolean point = (size & 1) == 1;
			size = size >> 1;
			if (size == 0 && !point) {
				return;
			}
			int p = i - 1;
			while (size > 0) {
				cs[p--] = 'L';
				size--;
			}
			if (point) {
				cs[p] = '.';
			}
		}
	}

	@Override
	public void debug1() {
		HelpDebug.compare("LL.RR.LLRRLL..", this.pushDominoes(".L.R...LR..L.."));

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
